package com.xinwei.leetcode.链表;
// https://leetcode-cn.com/problems/linked-list-cycle/
public class _141_环形链表 {
    public boolean hasCycle(ListNode head) {
        // 快慢指针：定义一个慢指针（只走一步），一个快指针（走两步），如果链表中有环，则两个指针必定相遇
        if (head == null || head.next == null) return false;
        ListNode slowNode = head;
        ListNode fastNode = head.next;
        while (null != fastNode && fastNode.next != null){
            slowNode = slowNode.next;
            fastNode = fastNode.next.next;
            if (slowNode == fastNode) return true;
        }
        return false;
    }

}
